/*
 * NeoBitFont - second attempt at bitmapped fonts using Bitmap Font Builder
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package flash3f;
// how do I want to make a new font?
// 1. subclass NeoBitFont
// 2a. when init() is called, super() then add the bitmapdata and rework Glyph.new()
//    so that it knows what font it's working off of
// 2b. then set the kerning for each glyph
// 3. use the subclass the way I use BitFont now

import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Point;
import flash.geom.Rectangle;
import StringTools;

class NeoBitFont
{

	var baseimage : Bitmap;
	var glyphs : IntHash<Glyph>;
	var base : Int;
	var lineheight : Int;
	
	public function new(bitmapdata : BitmapData)
	{
		glyphs = new IntHash();
	
		baseimage = new Bitmap();
		baseimage.bitmapData = bitmapdata;
		
	}
	
	public function gen(input : Array<Dynamic>) : BitmapData
	{
		var imagelist : Array <Glyph> = new Array();
		var poslist : Array <Point> = new Array();
		var curposx : Float = 0;
	
		var text : String = "";
		
		for (i in input)
		{
			text += Std.string(i);
		}
		
		for (c in 0...text.length)
		{
			var code = text.charCodeAt(c);
			if (glyphs.exists(code))
			{
				var glyph = glyphs.get(code);
				imagelist.push(glyph);
				var kern : Int = 0;
				if (c>0 && glyph.kerning.exists(text.charCodeAt(c-1)))
					kern = glyph.kerning.get(text.charCodeAt(c-1));
				poslist.push(	new Point(curposx+glyph.xoff+kern, 
								glyph.yoff));
				curposx += glyph.xadvance;
			}
		}
		
		var result = new BitmapData(Std.int(curposx+glyphs.get(text.charCodeAt(text.length-1)).xadvance), lineheight, true, 0x000000);
		
		for (i in 0...imagelist.length)
		{
			result.copyPixels(	imagelist[i].bitmapData,
								imagelist[i].bitmapData.rect,
								poslist[i], imagelist[i].bitmapData, 
								new Point(0,0), true);
		}
		
		return result;
	}
	
}

class Glyph extends Bitmap
{

	public var xoff : Int;
	public var yoff : Int;
	public var xadvance : Int;
	public var kerning : IntHash<Int>;
	
	public function new(basebitmapdata : BitmapData, xval : Int, yval : Int, 
						width : Int, height : Int, xoff : Int, yoff : Int, 
						xadvance : Int)
	{
		super();
		if (height<1)
			height = 1;
		if (width<1)
			width = 1;
		this.bitmapData = new BitmapData(width, height+1);
		this.bitmapData.copyPixels(	basebitmapdata, 
									new Rectangle(xval,yval,width,height+1),
									new Point(0,0));
		this.xoff = xoff;
		this.yoff = yoff;
		this.xadvance = xadvance;
		kerning = new IntHash();
	}
	
}
